package server

import (
	"wolongauth/config"
	"wolongauth/errhelp"
	"wolongauth/global"

	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/logger"
	"github.com/gofiber/fiber/v2/middleware/recover"
	"github.com/gofiber/template/mustache"
)

func Run() {
	engine := mustache.New("./views", ".mustache")
	WebServer = fiber.New(fiber.Config{
		ErrorHandler: ErrHelperFunc,
		Views:        engine,
	})
	WebServer.Use(recover.New(), logger.New())
	WebServer.Use(CommonAuthMiddleWare)
	BindRoutes()
	if config.GetConfig().Https {
		if config.GetConfig().SendToHttps {
			go func() {
				StartSendToHttpsServer()
			}()
		}
		global.Logger.Info("正在启动Https Server...")
		err := WebServer.ListenTLS(":"+config.GetConfig().WebPort, config.GetConfig().Https_Cert, config.GetConfig().Https_Key)
		errhelp.ErrDoWithTagAndAbort(err, "WebServer Start TLS")
	} else {
		global.Logger.Info("正在启动Http Server...")
		err := WebServer.Listen(":" + config.GetConfig().WebPort)
		errhelp.ErrDoWithTagAndAbort(err, "WebServer Start")
	}
}
